package com.wondersgroup.fjzlpt.tbxt.dao.impl.tz;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.quick.framework.util.FormatDate;
import com.wondersgroup.fjzlpt.tbxt.dao.inter.tz.YltfwtzDAO;
import com.wondersgroup.fjzlpt.tbxt.util.ControllerUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据操作层
 * <p>
 * <p>
 * Created by sanma on 2017/9/18.
 */
@Repository
public class YltfwtzDAOImpl extends BaseJTDAOImpl implements YltfwtzDAO {

    @Override
    @Autowired
    @Qualifier("TB_TBXT_YLTFWTZ")
    public void setTb(TableBean tb){
        super.setTb(tb);
    }

    @Value("${common.WSGLJGLBDM}")
    private String GLJGLBDM;

    @Override
    public JSONObject queryPage(Map map) {
        ControllerUtils.pageFormat(map);
        JSONObject sqlObj = this.getPageSql(map);
        List sqlParams = (List)sqlObj.get("sqlParams");
        PageQuerySQLBean pageBean = super.checkAndCreatePageQuerySQLBean(map,sqlObj.getString("sql")," ORDER BY a.FWSJ ",sqlParams.toArray());
        return super.queryPage(pageBean, ResultHandlerConstant.JSONARRAY);
    }

    public JSONObject getPageSql(Map<String,String> map){
        JSONObject jsonObject = new JSONObject();
        StringBuffer sqlBuf = new StringBuffer();
        sqlBuf.append("select * from ");
        sqlBuf.append(this.tb.getTableName()).append(" a ");
        sqlBuf.append("where 1=1 ");

        List sqlParams = new ArrayList();
        String value = map.get("JGLB");
        if(!StringUtils.isEmpty(value) && value.indexOf(GLJGLBDM) != -1){
            //管理机构
            sqlBuf.append("and a.XZQHDM like ? ");
            sqlParams.add(map.get("XZQHDM") + "%");
        }else if(!StringUtils.isEmpty(value = map.get("PTJGID"))){
            //非管理机构-只查询本机构
            sqlBuf.append("and a.PTJGID = ? ");
            sqlParams.add(value);
        }
        //服务名称
        value = map.get("FWMC");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.FWMC like ? ");
            sqlParams.add("%" + value + "%");
        }
        //提供服务机构
        value = map.get("TGFWJG");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.TGFWJG like ? ");
            sqlParams.add("%" + value + "%");
        }
        //服务时间段
        value = map.get("FWSJ_start");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.FWSJ >= ? ");
            sqlParams.add(FormatDate.getDateY_M_D(value));
        }
        value = map.get("FWSJ_end");
        if(!StringUtils.isEmpty(value)){
            sqlBuf.append("and a.FWSJ <= ? ");
            sqlParams.add(FormatDate.getDateY_M_D(value));
        }
        jsonObject.put("sql",sqlBuf.toString());
        jsonObject.put("sqlParams",sqlParams);
        return jsonObject;
    }

}
